<template>
    <div>
        <div class="search-con search-con-top">
            所属节：
            <Select v-model="searchData.node_id" filterable style="width: 150px;">
                <Option :value="item.id" v-for="item in arr" :key="item.id">{{ item.title }}</Option>
            </Select>
            节名：
            <Input v-model="searchData.title" style="width: 150px"></Input>
            <Button @click="search" type="primary" icon="ios-search" style="margin-left: 10px;">搜索</Button>
            <Button @click="reset" type="warning" icon="ios-refresh" style="margin-left: 10px;">刷新</Button>
            <Button slot="extra" @click="edit(false)" type="success" style="margin-left: 10px;">添加</Button>
        </div>
        <br>
        <Table :columns="columns" :data="list" @on-sort-change="customSort" :highlight-row="true" max-height="800" border
            stripe>
            <template slot-scope="{ row , index}" slot="imgSlot">
                <show-img :imgUrl="row.img"></show-img>
            </template>
            <template slot-scope="{ row , index}" slot="makeSlot">
                <Tooltip content="编辑" style="margin-left: 10px;" placement="top">
                    <Icon type="ios-create" size="24" color="#2d8cf0" style="cursor: pointer;" @click="edit(row)" />
                </Tooltip>
                <Tooltip content="删除" style="margin-left: 10px;" placement="top">
                    <Icon type="md-trash" size="24" color="#ed4014" style="cursor: pointer;" @click.stop="del(row.id)" />
                </Tooltip>
            </template>
        </Table>
        <br>

        <Paginate :total="total" @on-change="changePage" @on-page-size-change="changeSize" :current="searchData.page">
        </Paginate>


        <Modal title="添加" width="40" v-model="showAdd" :mask-closable="false">
            <Form ref="formValidate" :model="formItem" :label-width="75">
                <FormItem label="所属节">
                    <Select v-model="formItem.node_id" filterable>
                        <Option :value="item.id" v-for="item in arr" :key="item.id">{{ item.title }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="课程名">
                    <Input v-model="formItem.title" placeholder=""></Input>
                </FormItem>
                <FormItem label="封面图" class="item">
                    <MyUpload :maxNumber="1" @getImages="getImg" :defaultImags="[formItem.img]"></MyUpload>
                </FormItem>
                <FormItem label="视频" class="item">
                    <MyUpload :maxNumber="1" @getImages="getImg1" :defaultImags="[formItem.video]" :maxSize="102400"
                        :uploadType="2"></MyUpload>
                </FormItem>
                <FormItem label="视频时长">
                    <Input v-model="formItem.time"></Input>
                </FormItem>
                <FormItem label="排序">
                    <Input v-model="formItem.sort" placeholder="从小到大排序" type="number"></Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="primary" size="large" long @click="addQuestion">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import Paginate from "_c/paginate";
import MyUpload from "_c/my-upload"
import RichText from "_c/rich-text"
import ShowImg from '_c/show_img'
export default {
    name: "video_chapter",
    components: {
        RichText, MyUpload, ShowImg, Paginate
    },
    data() {
        return {
            searchData: {
                page: 1,
                limit: 10
            },
            // 列表
            columns: [
                {
                    title: '节名',
                    key: 'node_title',
                    align: 'center',
                },
                {
                    title: '课程名',
                    key: 'title',
                    align: 'center',
                },
                {
                    title: '图片',
                    align: "center",
                    key: 'img',
                    slot: "imgSlot"
                },
                {
                    title: '时长',
                    align: "center",
                    key: 'time',
                },
                {
                    title: '排序',
                    key: 'sort',
                    align: 'center',
                },
                {
                    title: '创建时间',
                    key: 'create_time',
                    align: 'center',
                },
                {
                    title: '操作',
                    slot: 'makeSlot',
                    align: 'center',
                },
            ],
            // 数据
            list: [],
            total: 0,
            formItem: {},
            showAdd: false,
            arr: [],
        }
    },
    methods: {
        getImg(items) {
            items.forEach(item => {
                this.formItem.img = item.url;
            });
        },
        getImg1(items) {
            items.forEach(item => {
                this.formItem.video = item.url;
            });
        },
        getRichTextContent(value) {
            this.formItem.content = value;
        },
        // 查询管理员信息
        getList() {
            this.$axios('/admin/video/course/getList', this.searchData).then(res => {
                this.list = res.data;
                this.total = res.total;
            });
        },
        search() {
            this.searchData.page = 1;
            this.getList();
        },
        reset() {
            this.searchData = {
                page: 1,
                limit: 10,
            };
            this.getList();
        },
        changePage(page) {
            this.searchData.page = page;
            this.getList();
        },
        changeSize(size) {
            this.searchData.limit = size;
            this.getList();
        },
        customSort(column, key, order) {
            this.searchData.order_by = column.key;
            this.searchData.sort = column.order;
            this.getList();
        },
        addQuestion() {
            this.$axios('/admin/video/course/save', this.formItem).then(res => {
                this.$Message.success('保存成功');
                this.formItem = {};
                this.showAdd = false;
                this.getList();
            });

        },
        edit(row) {
            if (row) {
                this.formItem = { ...row }
            } else {
                this.formItem = {
                    id: 0,
                    title: '',
                    sort: '',
                    img: '',
                    video: '',
                }
            }
            this.showAdd = true;
        },
        del(id) {
            this.$axios('/admin/video/course/del', { id: id }).then(res => {
                this.$Message.success('删除成功');
                this.getList();
            });
        },
        getArr() {
            this.$axios('/admin/video/node/getArr').then(res => {
                this.arr = res;
            });
        },
    },
    created() {
        this.getList();
        this.getArr();
    }
}
</script>

